package com.topscomm.pm.script;

import cn.hutool.core.date.DateUtil;
import com.topscomm.basic.util.SQLAdapter;
import com.topscomm.cbo.pojo.CboApprovalNodeInsEntity;
import com.topscomm.pm.PmScript;
import com.topscomm.pm.pojo.PmProjectChangeEntity;
import com.topscomm.pm.pojo.PmProjectEntity;
import com.topscomm.pm.service.IPmCalendarService;
import com.topscomm.pm.service.impl.PmCalendarService;
import com.topscomm.pm.service.impl.PmProjectChangeService;
import com.topscomm.pm.service.impl.PmProjectService;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.SpringUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.tap.ITapService;
import com.topscomm.tap.TapEntity;
import org.apache.commons.collections4.map.HashedMap;

import java.util.Map;

public class PmProjectChangeScript extends PmScript{


	/**
	 * 项目基本信息变更审批结束后回写项目信息
	 * @param service
	 * @param entity
	 * @throws Exception
	 */
	public void projectChangeApproval(ITapService service, TapEntity entity) throws Exception {
		// 审批节点实例
		CboApprovalNodeInsEntity nodeins = (CboApprovalNodeInsEntity) entity;
		String sourceid = ConvertUtil.convertToString(nodeins.getSourceid());
		PmProjectChangeService pmProjectChangeService = (PmProjectChangeService)SpringUtil.getBean("pmProjectChangeService");
		PmProjectService pmProjectService = (PmProjectService)SpringUtil.getBean("pmProjectService");
		//查询单据具体信息
		Map<String, Object> pmProjectChange = pmProjectChangeService.queryMapById(sourceid);
		//审批人id
		long approvaluserid = nodeins.getApprovaluserid();
		//更新项目信息表的相关字段
		Map<String, Object> updateParam = new HashedMap<>();
		updateParam.put("id", ConvertUtil.convertToString(pmProjectChange.get("projectid")));
		updateParam.put("customername",ConvertUtil.convertToString(pmProjectChange.get("customername2")));
		updateParam.put("customercode", ConvertUtil.convertToString(pmProjectChange.get("customercode2")));
		updateParam.put("province", ConvertUtil.convertToString(pmProjectChange.get("province2")));
		updateParam.put("name",  ConvertUtil.convertToString(pmProjectChange.get("name2")));
		updateParam.put("background", ConvertUtil.convertToString(pmProjectChange.get("background2")));
		updateParam.put("projectlevel", ConvertUtil.convertToString(pmProjectChange.get("projectlevel2")));
		updateParam.put("ownerorgname", ConvertUtil.convertToString(pmProjectChange.get("ownerorgname2")));
		updateParam.put("ownerorgcode", ConvertUtil.convertToString(pmProjectChange.get("ownerorgcode2")));
		updateParam.put("ownerdeptname", ConvertUtil.convertToString(pmProjectChange.get("ownerdeptname2")));
		updateParam.put("ownerdeptcode", ConvertUtil.convertToString(pmProjectChange.get("ownerdeptcode2")));
		updateParam.put("projectmanagername", ConvertUtil.convertToString(pmProjectChange.get("projectmanagername2")));
		updateParam.put("projectmanagercode",ConvertUtil.convertToString(pmProjectChange.get("projectmanagercode2")));
		updateParam.put("areamanagername", ConvertUtil.convertToString(pmProjectChange.get("areamanagername2")));
		updateParam.put("areamanagercode", ConvertUtil.convertToString(pmProjectChange.get("areamanagercode2")));
		updateParam.put("reason", ConvertUtil.convertToString(pmProjectChange.get("reason2")));
		updateParam.put("category", ConvertUtil.convertToString(pmProjectChange.get("category2")));
		updateParam.put("overview", ConvertUtil.convertToString(pmProjectChange.get("overview2")));
		updateParam.put("projectmoney", ConvertUtil.convertToString(pmProjectChange.get("projectmoney2")));
		updateParam.put("planstarttime", ConvertUtil.convertToString(pmProjectChange.get("planstarttime2")));
		// 如果计划开工时间有更改则需要调用日历同步项目日历
		IPmCalendarService pmCalendarService=(PmCalendarService)SpringUtil.getBean("pmCalendarService");
		if (!ConvertUtil.convertToString(pmProjectChange.get("planstarttime")).equals(ConvertUtil.convertToString(pmProjectChange.get("planstarttime2")))) {
			// 如果计划开始日期往后移则需要把新旧日期之间的日历删除
			if (DateUtil.parse(ConvertUtil.convertToString(pmProjectChange.get("planstarttime2"))).getTime()>DateUtil.parse(ConvertUtil.convertToString(pmProjectChange.get("planstarttime"))).getTime()) {
				// 旧开始日期至新开始日期之间的项目日历需要删除，例如：旧开始日期为  1月1号，修改后的开始日期为 1月15号，那么1月1号到1月14号的项目日历需要删除
				StringBufferProxy deleteWherSql = new StringBufferProxy();
				deleteWherSql.appendSingle("calendardate >={0} and calendardate<={1}", SQLAdapter.toDateLong(ConvertUtil.convertToString(pmProjectChange.get("planstarttime2")).substring(0,10) + " 00:00:00"),SQLAdapter.toDateLong(ConvertUtil.convertToString(pmProjectChange.get("planstarttime")).substring(0,10) + " 23:59:59"));
				pmCalendarService.deleteByWhere(deleteWherSql.toString());
			}else {
				// 如果计划开始日期往前移则需要添加新旧日期之间的日历
				PmProjectEntity pmProjectEntity = pmProjectService.queryById(ConvertUtil.convertToString(pmProjectChange.get("projectid")));
				Map<String, Object> insertParamMap = new HashedMap<>();
				insertParamMap.put("projectid", ConvertUtil.convertToString(pmProjectChange.get("projectid")));
				insertParamMap.put("projectcode", pmProjectEntity.getCode());
				insertParamMap.put("starttime", ConvertUtil.convertToString(pmProjectChange.get("planstarttime2")));
				insertParamMap.put("endtime", ConvertUtil.convertToString(pmProjectChange.get("planstarttime")));
				pmCalendarService.insertBatchCalendar(insertParamMap);
			}
		}
		updateParam.put("planendtime",ConvertUtil.convertToString(pmProjectChange.get("planendtime2")));
		updateParam.put("isincludeproducts", ConvertUtil.convertToString(pmProjectChange.get("isincludeproducts2")));
		updateParam.put("productdetails", ConvertUtil.convertToString(pmProjectChange.get("productdetails2")));
		updateParam.put("ispurchase", ConvertUtil.convertToString(pmProjectChange.get("ispurchase2")));
		updateParam.put("purchasedescription", ConvertUtil.convertToString(pmProjectChange.get("purchasedescription2")));
		updateParam.put("mandays", ConvertUtil.convertToString(pmProjectChange.get("mandays2")));
		pmProjectService.update(updateParam);
	}

	/**
	 * @author: wudaliang
	 * @date: 2021-09-27
	 * @description:
	 *  需要判断在ERP中新的组织下是否存在该项目号，如果没有的话，需要新增一个(为了解决项目开票时，发票所属组织下没有该项目号)
	 * @return: void
	 * @modify:
	 */
	public void createNewErpItem(ITapService service, TapEntity entity) throws Exception {
		// 审批节点实例
		CboApprovalNodeInsEntity nodeins = (CboApprovalNodeInsEntity) entity;
		String sourceid = ConvertUtil.convertToString(nodeins.getSourceid());
		PmProjectChangeService pmProjectChangeService = (PmProjectChangeService)SpringUtil.getBean("pmProjectChangeService");
		PmProjectService pmProjectService = (PmProjectService)SpringUtil.getBean("pmProjectService");
		//查询单据具体信息
		PmProjectChangeEntity pmProjectChangeEntity = pmProjectChangeService.queryById(sourceid);
		String ownerorgcode2 = pmProjectChangeEntity.getOwnerorgcode2();
		PmProjectEntity pmProjectEntity = pmProjectService.queryById(pmProjectChangeEntity.getProjectid());
		String projectcode = pmProjectEntity.getCode();
		//如果不存在
		if(!pmProjectChangeService.judgeExistsErpItemCode(ownerorgcode2,projectcode)){
			pmProjectChangeService.pmChangeAddErpItem(pmProjectEntity,pmProjectChangeEntity);
		}
	}
}
